<?php

namespace Home\Controller;
use Think\Controller;

//前台公共控制器，判断首页用户是否登录，是否继续操作的权限
class CommonController extends Controller {

    Public function _initialize(){ //构造函数 虽然b类继承了a类，但是输出结果证明程序只是执行了b类的构造函数，而没有自动执行父类的构造函数。

        //username??
        // if(!isset($_SESSION[C('uid')]) || !isset($_SESSION[C('username')])) {
        //  $this->redirect('Login/login');
        // }


    /*  $notAuth = in_array(MODULE_NAME, explode(',',C('NOT_AUTH_MODULE'))) || in_array(ACTION_NAME, explode(',', C('NOT_AUTH_ACTION')));

        if(C('USER_AUTH_ON' )&& !$notAuth){
            //注意调用方法,且GROUP_NAME常量已废除，默认值为MODULE_NAME
            \Org\Util\Rbac::AccessDecision() || $this->error('没有权限');*/
        }

        //
        public function __construct(){ //构造函数   构造后其他控制器可以得到common控制器内容
        parent::__construct();
        $cateid=I('cateid');
        $slinkid=I('cateid');
      // dump($cateid);
      // dump($slinkid);
        if($arid){
            $slink=D('slink');
            $slinks=$slink->find($slinkid);  //s
            $cateid=$slinks['cateid'];   //超链对应的分类的id
        //dump($cateid);
        }
        if($cateid){$this->selftop($cateid);}//
        if($cateid){
            $pres=$this->getparent($cateid);
            $this->assign('pres',$pres);
        }
        $this->assign('pres',$pres);
        //$this->getcate($cateid);
          $this->nav();  
        $this->link(); 
      }
        public function getparent($cateid){
        $res=array();
        $cate=D('cate');
        while ($cateid) {
            $cates=$cate->find($cateid);
            $res[]=array(
                'id'=>$cates['id'],
                'name'=>$cates['name'],
                'type'=>$cates['type']
                );
            $cateid=$cates['pid'];
        }

        return array_reverse($res);
   }

    
        //获取分类,首页展示@kw
            public function getcate($cateid){

                if($cateid){
                    $cate=D('cate');
                    $cates=$cate->find($cateid);
                    $catesons=$cate->where('pid='.$cates['id'] )->select();//子栏目  id相同，字段怎么相同

              //var_dump($catesons);
                    if($cates['pid']==0 && !$catesons){
                        $cates=$cate->where("id=240")->find();
                        $this->assign('contro',true); //如果是顶级栏目 分配一个ture 给contro

                    }
                    $this->assign('catesons',$catesons);

                    if($cates['class']==1){
                        $son2=$cate->where('pid='.$cates['id'])->select();
                //dump($son2);
                        $this->assign('son2',$son2);
                        $this->assign('son3',null);
                        $this->assign('topname',$cates['name']);

                    }

                    if($cates['class']==2){
                        $topcates=$cate->where('id='.$cates['pid'])->find();
               // dump($topcates);
                        $son2=$cate->where('pid='.$topcates['id'])->select();
                        $son3=$cate->where('pid='.$cates['id'])->select();
                        $this->assign('son2',$son2);
                        $this->assign('son3',$son3);
                        $this->assign('topname',$topcates['name']);
                    }


                    if($cates['class']==3){
                        $upercates=$cate->where('id='.$cates['pid'])->find();//三级栏目对应的二级栏目
                        $topcates=$cate->where('id='.$upercates['pid'])->find();
                        $son3=$cate->where('pid='.$upercates['id'])->select();
                        $son2=$cate->where('pid='.$topcates['id'])->select();
                        $this->assign('son2',$son2);
                        $this->assign('son3',$son3);
                        $this->assign('topname',$topcates['name']);
                        $this->assign('son3pid',$upercates['id']);

                    }



                }
          }

                 //获取二级分类与搜索词$kw相同的字段的id
     






            /**
             * selftop 文章所属的栏目自己的顶级分类
             */
                public function selftop($cateid){
                $cate=D('cate');
                $cates=$cate->find($cateid);

                if($cates['class']==1){
                    $selftop=$cates['name']; //可以获取到一级分类的名称

                    if($kw=I('kws')){ // 2.获取关键字$kw   kw不为空
               

               }


                }elseif($cates['class']==2){
                    $cates=$cate->where('id='.$cates['pid'])->find();
                   // dump($cates);
                    $selftop=$cates['name'];
                   // dump($selftop);
                }else{
                    $cates=$cate->where('id='.$cates['pid'])->find();
                    $cates=$cate->where('id='.$cates['pid'])->find();
                    $selftop=$cates['name'];
                }

                $this->assign('selftop',$selftop);
            }

            public function nav(){
                $cate=D('cate');
                $where=array('pid'=>0,'pc'=>1);
                $navres=$cate->field('id,name,type')->order('sort asc')->where($where)->select();
                $this->assign('navres',$navres);

               //var_dump($navres);
            }

                public function link(){
        $link=D('link');
        $linkres=$link->field('title,url')->select();
        $this->assign('linkres',$linkres);
    }

    






    }

